﻿
using AMapPlugin.Dtos;
using AMapPlugin.Handler;
using AMapPlugin.HttpApi;
using Microsoft.Extensions.DependencyInjection;
using Refit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AMapPlugin
{
    public static class ServiceExtension
    {
        /// <summary>
        /// 添加高德插件
        /// </summary>
        /// <param name="services"></param>
        /// <param name="opt"></param>
        /// <returns></returns>
        public static IServiceCollection AddAmapPlugin(this IServiceCollection services, Action<AmapOptions> opt)
        {
            var amapOptions = new AmapOptions();
            opt.Invoke(amapOptions);
            // 注册 AmapOptions 为单例服务，以便 AuthKeyHandler 使用
            services.AddSingleton(amapOptions);
            services.AddTransient<AuthKeyHandler>();
            services.AddRefitClient<IAmapApi>()
             .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://restapi.amap.com"))
             .AddHttpMessageHandler<AuthKeyHandler>();

            services.AddSingleton<IAmapPlugin, AmapPlugin>();
            return services;
        }

    }
}
